<?php
/**
 * Created by PhpStorm.
 * User: frankpian
 * Date: 17-12-7
 * Time: 下午7:11
 */

use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Regex as RegexValidator;
use Phalcon\Validation\Validator\Between;
use Phalcon\Validation\Validator\StringLength;

class VersionController extends CloudControllerBase
{

    /******************************    版本管理    ***********************************************************/
    public function listAppVersionAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT A.id, A.version, A.code, A.mini_code, A.url, A.description, A.type AS app_type, A.created_at FROM app_version AS A)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "version" => "主版本号",
                    "code" => "内部版本代码",
                    "mini_code" => "可用最低版本代码",
                    "url" => "文件下载链接",
                    "description" => "版本描述",
                    "app_type" => "应用类型",
                    "created_at" => "创建时间"
                ), $table
                    , array(
                        ["id", "="],
                        ["version", "like", "%", "%"],
                        ["code", "="],
                        ["mini_code", "="],
                        ["app_type", "="],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"]
                    ),"created_at desc","id");
            };
                break;
        }
        $this->view->setVars(array("planeTypeList" => $this->getPlaneModelInfo()));
    }

    public function detailAppVersionAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->transferErrorPage("权限不足", "");
        }
        $keyId = intval($this->getQuery("id"));
        $sql = "(SELECT *, type AS app_type FROM app_version
            WHERE id=$keyId)";
        $this->generateDetailData(array(
            "table" => $sql,
            "column" => array(
                ["version", "主版本号", ""],
                ["radio_version", "电台版本号", ""],
                ["project_version", "智航线版本号", ""],
                ["flight_project_version", "智飞行版本号", ""],
                ["smart_check_version", "智检图版本号", ""],
                ["smart_map_version", "智拼图版本号", ""],
                ["smart_process_version", "智理图版本号", ""],
                ["sha1", "文件sha1验证码", ""],
                ["code", "内部版本代码", ""],
                ["url", "文件下载链接", "disabled", true, "", "text-area", "3"],
                ["url2", "电台文件下载链接", "", true, "", "text-area", "3"],
                ["param", "飞控参数", "", true, "", "text-area", "8"],
                ["description", "版本描述", "", true, "", "text-area", "3"],
                ["app_type", "应用类型", "", true, "", "select", array("windows客户端" => 0, "IOS客户端" => 1, "F200飞控与电台固件" => 2, "F1000飞控与电台固件" => 3, "F300飞控与电台固件" => 4, "D200飞控与电台固件" => 5, "基准站固件" => 6)],
                ["created_at", "创建时间", ""],
                ["id", "", "", false],
            ),
            "title" => "ID",
            "titleKey" => "id",
            "keyName" => "id",
            "where" => "id=$keyId",
        ));
    }

    /******************************    windwos差分更新管理    ***********************************************************/
    public function listAppVersionWinAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT a.id, a.version, a.project_version, a.flight_project_version, a.smart_check_version, a.smart_map_version, a.smart_process_version, a.code, a.description, a.created_at FROM app_version_win AS a)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "version" => "主版本号",
                    "project_version" => "智航线版本号",
                    "flight_project_version" => "智飞行版本号",
                    "smart_check_version" => "智检图版本号",
                    "smart_map_version" => "智拼图版本号",
                    "smart_process_version" => "智理图版本号",
                    "code" => "内部版本代码",
                    "description" => "版本描述",
                    "created_at" => "更新时间"
                ), $table
                    , array(
                        ["id", "="],
                        ["version", "like", "%", "%"],
                        ["project_version", "like", "%", "%"],
                        ["flight_project_version", "like", "%", "%"],
                        ["smart_check_version", "like", "%", "%"],
                        ["smart_map_version", "like", "%", "%"],
                        ["smart_process_version", "like", "%", "%"],
                        ["code", "="],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"]
                    ),"created_at desc","id");
            };
                break;
        }
        $this->view->setVars(array("planeTypeList" => $this->getPlaneModelInfo()

            )

        );
    }

    public function detailAppVersionWinAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->transferErrorPage("权限不足", "");
        }
        $keyId = intval($this->getQuery("id"));
        $sql = "(SELECT * FROM app_version_win
            WHERE id=$keyId)";
        $this->generateDetailData(array(
            "table" => $sql,
            "column" => array(
                ["version", "主版本号", ""],
                ["project_version", "智航线版本号", ""],
                ["flight_project_version", "智飞行版本号", ""],
                ["smart_check_version", "智检图版本号", ""],
                ["smart_map_version", "智拼图版本号", ""],
                ["smart_process_version", "智理图版本号", ""],
                ["code", "内部版本代码", ""],
                ["description", "版本描述", "", true, "", "text-area", "2"],
                ["file_list", "文件列表", "", true, "", "text-area", "20"],
                ["created_at", "创建时间", ""],
                ["id", "", "", false],
            ),
            "title" => "ID",
            "titleKey" => "id",
            "keyName" => "id",
            "where" => "id=$keyId",
        ));
    }

    /******************************    windows更新程序    **************************************************/
    public function listAppUpdateToolAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT * FROM app_update_tool)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "version" => "版本号",
                    "code" => "内部版本代码",
                    "description" => "版本描述",
                    "created_at" => "创建时间"
                ), $table
                    , array(
                        ["id", "="],
                        ["version", "like", "%", "%"],
                        ["code", "="],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"]
                    ), "created_at desc", "id");
            };
                break;
        }
        $this->view->setVars(array("planeTypeList" => $this->getPlaneModelInfo()

            )

        );
    }

    public function detailAppUpdateToolAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->transferErrorPage("权限不足", "");
        }
        $keyId = intval($this->getQuery("id"));
        $sql = "(SELECT * FROM app_update_tool
            WHERE id=$keyId)";
        $this->generateDetailData(array(
            "table" => $sql,
            "column" => array(
                ["version", "主版本号", ""],
                ["code", "内部版本代码", ""],
                ["description", "版本描述", "", true, "", "text-area", "2"],
                ["file_list", "文件列表", "", true, "", "text-area", "20"],
                ["created_at", "创建时间", ""],
                ["id", "", "", false],
            ),
            "title" => "ID",
            "titleKey" => "id",
            "keyName" => "id",
            "where" => "id=$keyId",
        ));
    }

    /******************************    数据处理软件更新程序    **************************************************/
    public function listAppVersionDataAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT * FROM app_version_data)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "version" => "版本号",
                    "is_default" => "默认版本",
                    "made_at" => "软件发布时间",
                    "created_at" => "上传时间"
                ), $table
                    , array(
                        ["id", "="],
                        ["version", "like", "%", "%"],
                        ["is_default", "="],
                        ['made_at_start', '>=', '', '', "made_at"],
                        ['made_at_end', '<=', '', '', "made_at"],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"]
                    ), "created_at desc", "id");
            };
                break;
        }
        $this->view->setVars(array("planeTypeList" => $this->getPlaneModelInfo()

            )

        );
    }

    public function detailAppVersionDataAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->transferErrorPage("权限不足", "");
        }
        $keyId = intval($this->getQuery("id"));
        $sql = "(SELECT * FROM app_version_data
            WHERE id=$keyId)";
        $this->generateDetailData(array(
            "table" => $sql,
            "column" => array(
                ["version", "版本号", ""],
                ["made_at", "软件发布时间", ""],
                ["created_at", "创建时间", ""],
                ["file_list", "文件列表", "", true, "", "text-area", "20"],
                ["id", "", "", false],
            ),
            "title" => "ID",
            "titleKey" => "id",
            "keyName" => "id",
            "where" => "id=$keyId",
        ));
    }

    /******************************    数据处理软件授权管理    *******************************************/
    public function listDataLicenceAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT * FROM data_licence)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "key_id" => "加密狗ID",
                    "licence_id" => "授权ID",
                    "created_at" => "创建时间",
                    "updated_at" => "更新时间",
                    "expired_at" => "过期时间",
                    "company" => "用户单位"
                ), $table
                    , array(
                        ["id", "="],
                        ["key_id", "like", "%", "%"],
                        ["licence_id", "like", "%", "%"],
                        ["company", "like", "%", "%"],
                        ['updated_at_start', '>=', '', '', "updated_at"],
                        ['updated_at_end', '<=', '', '', "updated_at"],
                        ['expired_at_start', '>=', '', '', "expired_at"],
                        ['expired_at_end', '<=', '', '', "expired_at"],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"],
                    ), "created_at desc", "id");
            };
                break;
        }
        $this->view->setVars(array("planeTypeList" => $this->getPlaneModelInfo()

            )

        );
    }

    public function detailDataLicenceAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->transferErrorPage("权限不足", "");
        }
        switch ($this->getRequestAction()) {
            case $this::REQUEST_ACTION_UPDATE:{
                $this->checkRequestParams(array(
                    array(
                        "key" => "id",
                        "type" => "presence",
                        "message" => "id为空"),
                ));
                $id = $this->getPost("id");
                $this->generateUpdateData(array(
                    "table" => "data_licence",
                    "fieldList" => array("expired_at", "company", "is_formal", "phone", "area", "buy_at", "comment"),
                    "where" => " id=$id"
                ));
            };
                break;
            default: {
                $keyId = intval($this->getQuery("id"));
                $sql = "(SELECT * FROM data_licence
            WHERE id=$keyId)";
                $this->generateDetailData(array(
                    "table" => $sql,
                    "column" => array(
                        ["key_id", "加密狗ID", "disabled"],
                        ["licence_id", "授权ID", "disabled"],
                        ["created_at", "创建时间", "disabled"],
                        ["updated_at", "更新时间", "disabled"],
                        ["expired_at", "过期时间", "", true, "", "date", ""],
                        ["company", "用户单位", ""],
                        ["is_formal", "正式", "", true, "", "select", array("否" => 0, "是" => 1)],
                        ["phone", "手机号码", ""],
                        ["area", "所属区域", ""],
                        ["buy_at", "购买时间", "", true, "", "date", ""],
                        ["pc_info", "最后一次激活的PC信息", "disabled", true, "", "text-area", "2"],
                        ["comment", "备注", ""],

                        ["id", "", "", false],
                    ),
                    "title" => "ID",
                    "titleKey" => "id",
                    "keyName" => "id",
                    "where" => "id=$keyId",
                ));
            }
        }
    }

    /******************************    视频地面站版本管理   *******************************************/
    public function addVideoGSVersionAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $userId=$this->getMonitorLoginUser()->userId;
        $action = $this->getRequestAction();
        switch ($action) {
            case $this::REQUEST_ACTION_ADD:{
                $this->checkRequestParams(array(
                    array(
                        "key" => "version",
                        "type" => "presence",
                        "message" => "输入版本号"),
                    array(
                        "key" => "code",
                        "type" => "presence",
                        "message" => "输入内部版本号"),
                    array(
                        "key" => "url",
                        "type" => "presence",
                        "message" => "文件未上传"),
                ));
                $last_id = $this->insertSqlData(array(
                    "table" => "video_gs_version",
                    "fieldList" => array("code", "version", "url", "force_update", "description"),
                    "addItems" => array("created_user_id" => $userId)
                ));
                if ($last_id>0){
                    $this->returnSuccess("文件上传成功");
                }else{
                    $this->returnError("上传失败！");
                }
            };
                break;
            default:
                break;
        }
    }

    public function listVideoGSVersionAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action)
        {
            case  $this::REQUEST_ACTION_DELETE: {
            };
                break;
            case  $this::REQUEST_ACTION_LIST: {
                $table = "(SELECT u.nickname, v.id, v.version, v.code, v.force_update, v.url, v.created_at, v.created_user_id FROM video_gs_version AS v
                           LEFT JOIN user AS u ON v.created_user_id=u.id)";
                $this->postQueryByPage(array(
                    "id" => "ID",
                    "version" => "版本号",
                    "code" => "内部版本号",
                    "force_update" => "是否强制升级",
                    "url" => "文件下载",
                    "nickname" => "上传人",
                    "created_at" => "创建时间"
                ), $table
                    , array(
                        ["code", "like", "%", "%"],
                        ["version", "like", "%", "%"],
                        ["force_update", "="],
                        ['created_at_start', '>=', '', '', "created_at"],
                        ['created_at_end', '<=', '', '', "created_at"]
                    ),"created_at desc","id");
            };
                break;
        }
    }

    public function detailVideoGSVersionAction()
    {
        if (!$this->isMonitorAdminUser()) {
            $this->returnError($this->lstr("OPERATION_AUTH_ERROR"));
        }
        $action = $this->getRequestAction();
        switch ($action) {
            case $this::REQUEST_ACTION_UPDATE:
            {
                $keyId = $this->getPost("id");
                $sqlId = $this->updateSqlData(array(
                    "table" => "video_gs_version",
                    "fieldList" => array("code", "version", "url", "force_update", "description"),
                    "where" => " id='$keyId' "
                ));
                $this->returnSuccess("发布或保存成功");
            }
                break;
            case $this::REQUEST_ACTION_DELETE:
            {
                $data = $this->getPost("filter_list");
                $keyId = json_decode($data, true)["id"];
                $data = $this->fetchOne("SELECT * FROM video_gs_version WHERE id=$keyId");
                if ($data["url"]){
                    $this->deleteFileCos($data["url"]);
                }
                $result = $this->deleteOne("video_gs_version", array("id"=>$keyId));
                if ($result>0){
                    $this->returnSuccess("删除成功！");
                }else{
                    $this->returnError("删除失败！");
                }
            }
                break;
            default:{
                $keyId = $this->getQuery("id");
                $sql = "SELECT * FROM video_gs_version WHERE video_gs_version.id=$keyId";
                $keyData = $this->fetchOne($sql);
                $this->view->setVars(array("data"=>$keyData));
            }
                break;
        }
    }

    public function publishVideoGSVersionAction()
    {
        $appData = $this->checkAppIdAndSecret();
        $this->checkRequestParams(array(
            array(
                "key" => "version",
                "type" => "presence",
                "message" => "输入版本号"),
            array(
                "key" => "code",
                "type" => "presence",
                "message" => "输入内部版本号"),
            array(
                "key" => "url",
                "type" => "presence",
                "message" => "url不存在"),
            array(
                "key" => "force_update",
                "type" => "presence",
                "message" => "是否强制升级"),
        ));
        $last_id = $this->insertSqlData(array(
            "table" => "video_gs_version",
            "fieldList" => array("code", "version", "url", "force_update", "description"),
            "addItems" => array("created_user_id" => $appData["admin_user_id"])
        ));
        if ($last_id){
            $this->returnSuccess("发布成功");
        }else{
            $this->returnError("发布失败！");
        }
    }

    public function getVideoGSUpdateAction()
    {
        $sql = "SELECT * from video_gs_version where code = 
(select max(code) AS rank FROM video_gs_version)";
        $data = $this->fetchOne($sql);
        if ($data){
            $this->returnJson($data);
        }else {
            $this->returnError("查询错误!");
        }

    }


    public function filedItemChanged($table, $name, $value)
    {
        switch($name){
            case "app_type": {
                switch($value){
                    case "0":
                        return "windows客户端";
                        break;
                    case "1":
                        return "iOS客户端";
                        break;
                    case "2":
                        return "F200飞控与电台固件";
                        break;
                    case "3":
                        return "F1000飞控与电台固件";
                        break;
                    case "4":
                        return "F300飞控与电台固件";
                        break;
                    case "5":
                        return "D200飞控与电台固件";
                        break;
                    case "6":
                        return "基准站固件";
                        break;
                }
            };
                break;
            case "url": {
                if($value){
                    return "<a href='" . $value . "'>下载文件</a>";
                }
            };
                break;
            case "is_default": {
                if($value=="1"){
                    return "是";
                }else{
                    return "否";
                }
            };
                break;
        }
        return $value;
    }

}